/*
 * Logger.java
 *
 * Created on April 20, 2006, 2:28 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Comm.Util;
import Experience.*;
import Experience.Screens.LogScreen;
import java.util.Date;

/**
 *
 * @author dmgreen
 */
public class Logger {
    
    private String myLog = "MAHI Log\n\n";
    
    public static Logger logObj = null;
    
    public static String MSG_DEBUG = "DEBUG";
    public static String MSG_INFO  = "INFO";
    public static String MSG_ERROR = "ERROR";
    
    /** Creates a new instance of Logger */
    public Logger() {
        
    }

    public static String getDate() {
        
        long cMillis = System.currentTimeMillis();
        Date cDate = new Date(cMillis);
        String date_string = ((Date) cDate).toString();
     
        int space_count = 4;
        int last_space_idx = 0;
        int next_space_loc = 0;
        
        try {
            while (space_count > 0) {
                next_space_loc = date_string.indexOf(" ", last_space_idx+1);
                last_space_idx = next_space_loc;
                space_count--;
            }
        }
        catch (Exception e) {
            return date_string;
        }
            
        return date_string.substring(0,next_space_loc);
        
        //return (Calendar.getInstance()).toString();
        
        //String date_str = cDate.toString();
        //date_str = date_str.substring(date_str.indexOf(" "),date_str.length());        
    }
     
    
    public void addLogMessage3(String msg, String msg_type) {
        myLog += getDate() + "\n" + msg + "\n\n";
    }
    
    public void addLogMessage(String msg, String msg_type) {
        if ( msg_type.equals(MSG_DEBUG) ) {
             
            Preferences my_prefs = Preferences.getPreferences();
            
            if (my_prefs != null) {
                if (!my_prefs.getDebugMode()) { return; }
            } else {
                return;
            }
        }
        
        //date is wrong !!
        if (myLog.length() == 50000) {
            // i believe this is 100K of data, which is more than you'd want to scroll
            // through, anyway.
            myLog = "";
        }
        myLog += getDate() + "\n" + msg + "\n\n";
        
        //HACK
        
        
        if (ExpUI.MY_MIDLET == null) { return; }
        
        LogScreen ls = ExpUI.MY_MIDLET.isLogScreenDisplayed();
        if (ls != null) {
           ls.append(getDate() + "\n" + msg + "\n\n");
        }
        
    }
    
    public String getLogMessages() {
        return myLog;
    }
    
    public static Logger createLog() {
        if (logObj == null) {
            logObj = new Logger();
        }
        return logObj;
    }
    
    public static Logger getLog() {
        return logObj;
    }
    
    public void clearLog() {
        myLog = "MAHI Log\n\n";
    }
    
}


